import req, { RequsetOptons } from "/@/api/req";
import {
  AddUserParams,
  GrantTypeEnum,
  LoginParams,
  LoginResult,
  TenantInfo,
  UserDetail,
  UserItem,
  UserState,
} from "./models";
import env from "/@/base/env";
import type { ID } from "/@/api/models";
import type { LockModel } from "/@/api/aiot/account/models";
import { AppTypeEnum } from "../application/models";

export enum Api {
  usersPage = "api/system/userTenantInfo/page",
  addUser = "api/system/userTenantInfo/addUser",
  updateUser = "api/system/userTenantInfo/updateUser",
  getUserTenantInfoByUserId = "api/system/userTenantInfo/getUserTenantInfoByUserId",
  removeUser = "api/system/userTenantInfo/removeUser",
  authorizedUserFunctions = "api/system/userTenantInfo/authorizedUserFunctions",
  lockUserByUserId = "api/system/userTenantInfo/lockUserByUserId",
  saveLoginLog = "api/system/oauthLogs/saveLoginLog",
  getTenantList = "api/v1/users/information/enterprises/list",
  updateUserState = "api/system/userTenantInfo/updateUserState",
}

export enum oauthApi {
  login = "api/system/oauth/login",
  getCaptchaImg = "api/authentication/oauth/captcha",
}

export function lockUserByUserId(data: LockModel) {
  return req.post<boolean>({
    url: Api.lockUserByUserId,
    data,
  });
}

export function addUser(data: AddUserParams) {
  return req.post<UserItem>({
    url: Api.addUser,
    data,
  });
}

export function getUserTenantInfoByUserId(params: { userId?: ID }, options: RequsetOptons) {
  return req.get<UserDetail>(
    {
      url: Api.getUserTenantInfoByUserId,
      params,
    },
    options
  );
}

export function authorizedUserFunctions(data: AddUserParams) {
  return req.post<boolean>({
    url: Api.authorizedUserFunctions,
    data,
  });
}

export function login(data: LoginParams) {
  return req.post<LoginResult>(
    {
      data,
      url: oauthApi.login,
    },
    {
      withToken: false,
      useDefaultFailHandle: false,
      useGlobalLoading: false,
    }
  );
}

export function getCaptchaImgUrl(key: string) {
  return env.baseUrl + oauthApi.getCaptchaImg + `?key=${key}`;
}

export function saveLoginLog() {
  return req.post(
    {
      url: Api.saveLoginLog,
      params: {
        grantType: GrantTypeEnum.PASSWORD,
        appType: AppTypeEnum.PC,
      },
    },
    {
      useGlobalLoading: false,
      useDefaultErrHandle: false,
      useDefaultFailHandle: false,
    }
  );
}

export function getTenantList() {
  return req.commonPost<TenantInfo[]>(Api.getTenantList, {}, { useGlobalLoading: false });
}

export function updateUserState(data: { state: UserState; userId: string }) {
  return req.commonPost<boolean>(Api.updateUserState, data, { useGlobalLoading: true });
}

// export function testInter() {
//   return req.put<any>(
//     {
//       url: "https://xenethw.network/api/accounts/account",
//       data: {
//         requestMsg:
//           "rZ4djzp55meM9wl/f7gxNLL9GhcDWQduESc5iNmo0mJvs3B/3yVQs5/8L1KX195Gf7SG4yj4UXPuwudTM7Gl20rfpuln79r4V08q1Xgt+Uc=",
//       },
//     },
//     {
//       withToken: false,
//     }
//   );
// }
